package org.scboy.rbac.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.cache.SystemConfig;
import org.scboy.common.request.Result;
import org.scboy.rbac.service.LoginSessionService;
import org.scboy.rbac.service.SystemService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("rbac/system")
@Menu("权限管理系统.系统管理")
public class SystemController {
	
	@Autowired
	SystemService  systemService;

	@Autowired
	LoginSessionService loginSessionService;

	@Autowired
	SystemConfig config;
	
	/**
	 * 分页查询菜单列表
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Permission("分页查询系统列表")
	public Result page(@RequestBody Model params){
		Page page=systemService.page(params);
		return Result.Success(page.getData(),page.getCount());	
	}
	
	/**
	 * 新增系统
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增系统")
	public Result add(@RequestBody Model params){
		if(systemService.add(params)){
			return Result.Success("RBAC_SYSTEM_ADD_SUCCESS");
		}else{
			return Result.Error("RBAC_SYSTEM_ADD_ERROR");
		}	
	}
	
	/**
	 * 更新系统
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新系统")
	public Result update(@RequestBody Model params){
		if(systemService.update(params)){
			return Result.Success("RBAC_SYSTEM_UPDATE_SUCCESS");
		}else{
			return Result.Error("RBAC_SYSTEM_UPDATE_ERROR");
		}	
	}
	
	/**
	 * 删除系统
	 * @param request
	 * @return
	 */
	@DeleteMapping("delete")
	@Permission("删除系统")
	public Result delete(String id){
		if(systemService.delete(id)){
			return Result.Success("RBAC_SYSTEM_DELETE_SUCCESS");
		}else{
			return Result.Error("RBAC_SYSTEM_DELETE_ERROR");
		}	
	}
	
	/**
	 * 启用禁用
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用")
	public Result checkState(String id){
		if(systemService.checkState(id)){
			return Result.Success("RBAC_SYSTEM_CHECKSTATE_SUCCESS");
		}else{
			return Result.Error("RBAC_SYSTEM_CHECKSTATE_ERROR");
		}
	}
	/**
	 * 改变显示状态
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("display")
	@Permission("改变显示状态")
	public Result display(String id){
		if(systemService.display(id)){
			return Result.Success("RBAC_SYSTEM_DISPLAY_SUCCESS");
		}else{
			return Result.Error("RBAC_SYSTEM_DISPLAY_ERROR");
		}
		
	}
	
	/**
	 * 获取系统信息
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取系统信息")
	public Result get(String id){
		return Result.Success(systemService.get(id),"");
	}
	
	/**
	 * 升序降序
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("sort")
	@Permission("升序降序")
	public Result sort(String id,int type){
		if(systemService.sort(id, type)){
			return Result.Success("RBAC_SYSTEM_SORT_SUCCESS");
		}else{
			return Result.Error("RBAC_SYSTEM_SORT_ERROR");
		}
	}

	@GetMapping("list")
	@Permission("根据登录人获取可访问的系统")
	public Result list(HttpServletRequest request) {
		String token=request.getHeader(config.getConfig("TOKEN_KEY"));
		return Result.Success(systemService.list(token));
	}
	
	@GetMapping("listTree")
	@Permission("菜单配置首次加载系统数据")
	public Result listTree() {
		return Result.Success(systemService.listTree());
	}
}
